package com.ufo.store.management.widget.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.ufo.store.management.R;
import com.ufo.store.management.utils.Utils;

import java.text.DecimalFormat;

/**
 * Created by taoli on 2018/1/19.
 */

public class ArcProgress extends View {
    private Paint textPaint, paint, paint1;
    private int viewWidth = 0;
    private int viewHeight = 0;
    private int progress = 0;
    private float radius = 0;
    private RectF rectF = new RectF();
    private float arcBottomHeight;

    private int default_max = 0;
    private final float default_arc_angle = 270;
    private float default_stroke_width = 0;
    private final int default_finished_color = Color.parseColor("#efefef");
    private int default_unfinished_color = Color.parseColor("#ffffff");
    private final int default_text_color = Color.parseColor("#ffffff");


    public ArcProgress(Context context) {
        this(context, null);
    }

    public ArcProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        default_stroke_width = Utils.dp2px(getResources(), 6);
        initPainters();
    }

    @Override
    public void invalidate() {
        initPainters();
        super.invalidate();
    }

    protected void initPainters() {
        textPaint = new TextPaint();
        textPaint.setColor(default_text_color);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(Utils.dp2px(getResources(), 10));

        paint = new Paint();
        paint.setColor(default_finished_color);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(default_stroke_width / 2);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);

        paint1 = new Paint();
        paint1.setAntiAlias(true);
        paint1.setDither(true);
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setStrokeCap(Paint.Cap.ROUND);
        paint1.setStrokeWidth(default_stroke_width);
        paint1.setColor(default_unfinished_color);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        if (this.progress > getMax()) {
            this.progress = getMax();
        }
        invalidate();
    }

    public int getMax() {
        return default_max;
    }

    public void setMax(int max) {
        if (max >= 0) {
            this.default_max = max;
            invalidate();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        rectF.set(default_stroke_width , default_stroke_width , viewWidth - default_stroke_width , viewHeight - default_stroke_width );
        radius = viewWidth / 2 - default_stroke_width;
        float radius0 = viewWidth / 2f;
        float angle = (360 - default_arc_angle) / 2f;
        arcBottomHeight = radius0 * (float) (1 - Math.cos(angle / 180 * Math.PI)) - 20;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float startAngle = 270 - default_arc_angle / 2f;
        float finishedSweepAngle = progress / (float) getMax() * default_arc_angle;
        float finishedStartAngle = startAngle;

        //花完整的大圆
        canvas.drawArc(rectF, startAngle, default_arc_angle, false, paint);
//        paint.setColor(finishedStrokeColor);
//        paint.setStrokeWidth(paint.getStrokeWidth()*2);
        //画进度半弧
//                int colors[] = new int[2];
//        colors[0] = getResources().getColor(R.color.app_key_color);
//        colors[1] = 0xFFFFFFFF;
//        //渐变进度条
//        LinearGradient shader = new LinearGradient(
//                0, 0,
//                (int) ((viewWidth - default_stroke_width) * 0.6), viewHeight - default_stroke_width,
//                colors,
//                null,
//                Shader.TileMode.CLAMP);
//        paint1.setShader(shader);
        canvas.drawArc(rectF, finishedStartAngle, finishedSweepAngle, false, paint1);

        double x = viewWidth / 2 - Math.cos(2 * Math.PI / 360 * (finishedSweepAngle - 45)) * radius;
        double y = viewWidth / 2 - Math.sin(2 * Math.PI / 360 * (finishedSweepAngle - 45)) * radius;
        paint1.setStyle(Paint.Style.FILL);
        paint1.setColor(Color.WHITE);
        //画进度上的小圆点
        canvas.drawCircle((float) x, (float) y, default_stroke_width, paint1);
        paint1.setStyle(Paint.Style.STROKE);

        float bottomTextBaseline = viewHeight - arcBottomHeight - (textPaint.descent() + textPaint.ascent()) / 2;
        DecimalFormat decimalFormat = new DecimalFormat("##,###.0");
        String str = "¥" + decimalFormat.format(default_max);
        canvas.drawText("¥0", viewWidth / 8 + textPaint.measureText("¥0"), bottomTextBaseline, textPaint);
        canvas.drawText( "今日 " + str, viewWidth / 8 * 7 - textPaint.measureText("今日 " + str), bottomTextBaseline, textPaint);
    }
}
